package com.backstage.common.uitil;

import java.util.Random;

/**
 * @Project authority
 * @Package com.backstage.common.uitil
 * @Data 2017/7/17上午 10:50.
 * @Aurhor 阮雪峰
 */
public class StringUtil {
    private StringUtil(){}

    public static String getRandomStr(int length){
        String CHARS = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
        boolean[] bools = new boolean[CHARS.length()];
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        int i = random.nextInt(CHARS.length());
        // 循环5次，即生成5个不同的字符
        for (int j = 0; j < length; j++) {
            // 如果这个位置的bools的值为true,说明这个位置的字符已经出现过来，需要重新产生一个随机数
            while (bools[i]) {
                i = random.nextInt(CHARS.length());
            }
            // 生成了一个随机数之后就把对应位置的bools的值改为true
            bools[i] = true;
            sb.append(CHARS.charAt(i));
        }
        return sb.toString();
    }
}
